//
//  IntroduceArtsVC.m
//  jingdongfang
//
//  Created by MTiMac02 on 2018/5/9.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IntroduceArtsVC.h"
#import "IntroduceArtCell.h"
#import "IGDrawIntroApi.h"
#import "DrawIntroModel.h"
#import "HomeHDTypeVC.h"

@interface IntroduceArtsVC ()
{
    UIImageView *backgroundImage;
    NSMutableArray *lstid;
    DrawIntroModel *intromodle;
}
@property(nonatomic,strong)IGDrawIntroApi *drawIntroApi;
@property(nonatomic,strong)NSString *image;
@property(nonatomic,strong)IGDrawIntroModel *introModel;
@property(nonatomic,strong)NSMutableArray*category;
@property(nonatomic,strong)NSString *content;
@end

@implementation IntroduceArtsVC
#pragma mark-懒加载
-(NSMutableArray *)category{
    if (!_category) {
        _category=[NSMutableArray array];
    }
    return _category;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"画单";
    self.tableView.frame=CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT-66-20);
    self.tableView.showsHorizontalScrollIndicator=NO;
    self.tableView.showsVerticalScrollIndicator=NO;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    backgroundImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    [backgroundImage setImage:[UIImage imageNamed:@"gtext-1"]];
    UIBlurEffect *beffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *view = [[UIVisualEffectView alloc] initWithEffect:beffect];
    view.frame = backgroundImage.bounds;
    [backgroundImage addSubview:view];
    [self.tableView setBackgroundView:backgroundImage];
     [self.tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]];
    [self.tableView registerNib:[UINib nibWithNibName:@"IntroduceArtCell" bundle:nil] forCellReuseIdentifier:@"IntroduceArtCell"];
    [self requestData];
    [self.tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
-(void)requestData{
    self.drawIntroApi=[[IGDrawIntroApi alloc]initWithDrawlistId:self.drawId];
    [self.drawIntroApi startWithDecodeClass:[IGDrawIntroResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGDrawIntroResp *resp=responseObject;
            IGDrawIntroModel *model=resp.data;
            [self setBackgroundImage];
            self.introModel=model;
            [backgroundImage sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"gtext-1"]];
            [self.category addObjectsFromArray:model.lstLabel];
            [self.tableView reloadData];
        }
    }];    
}
-(void)setBackgroundImage{
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"IntroduceArtsCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    if (indexPath.section==0) {
        if (indexPath.row==0) {
          //画单主图
        cell.backgroundColor=[UIColor clearColor];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        UIImageView *artimage=[[UIImageView alloc]init];
        artimage.frame=CGRectMake(0, 0, 150, 150);
        [artimage sd_setImageWithURL:[NSURL URLWithString:self.introModel.image] placeholderImage:[UIImage imageNamed:@"gtext-1"]];
        artimage.centerX=KSCREENWIDTH/2;
        UIImageView *eyeImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_look"]];
        eyeImage.frame=CGRectMake(0, 5, 15, 15);
        [artimage addSubview:eyeImage];
        //画单浏览量
        UILabel *nubmer=[[UILabel alloc]initWithFrame:CGRectMake(0, 5, 40, 15)];
        nubmer.text=[NSString stringWithFormat:@"%@万",self.introModel.browseNum];
        nubmer.font=[UIFont systemFontOfSize:11];
        nubmer.textColor=[UIColor c3Color];
        [nubmer sizeToFit];
        [artimage addSubview:nubmer];
        nubmer.right=artimage.width-3;
        eyeImage.right=nubmer.left-3;
        eyeImage.centerY=nubmer.centerY;
        [cell addSubview:artimage];
        UILabel *artname=[[UILabel alloc]initWithFrame:CGRectMake(0, artimage.bottom+21, KSCREENWIDTH, 17)];
        artname.textColor=[UIColor c3Color];
        artname.text=self.introModel.titile;
        artname.font=[UIFont font18];
        artname.textAlignment=NSTextAlignmentCenter;
        [cell addSubview:artname];
        return cell;
        }
        if (indexPath.row==1) {
            //上传者信息
            IntroduceArtCell *cell=[tableView dequeueReusableCellWithIdentifier:@"IntroduceArtCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.authorImage.image=[UIImage imageNamed:self.introModel.userImage];
            cell.uid=self.introModel.uId;
            [cell.authorNameBtn setTitle:self.introModel.userName forState:UIControlStateNormal];
            cell.backgroundColor=[UIColor clearColor];
            return cell;
        }
        if (indexPath.row==2) {
            //画单标签按钮
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.backgroundColor=[UIColor clearColor];
            lstid=[NSMutableArray array];
            NSUInteger w=50;
            for (int i = 0 ; i < self.category.count; i++) {
                UIButton *mapBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                intromodle=self.category[i];
                [lstid addObject: intromodle.labelId];
                mapBtn.tag = i+1000;
                mapBtn.clipsToBounds=YES;
                mapBtn.layer.borderWidth=1.0f;
                mapBtn.layer.cornerRadius=14.f;
                mapBtn.layer.borderColor=[UIColor c3Color].CGColor;
                mapBtn.contentEdgeInsets=UIEdgeInsetsMake(5, 10, 5, 10);
                mapBtn.titleLabel.font=[UIFont font14];
                [mapBtn setTitleColor:[UIColor c3Color] forState:UIControlStateSelected];
                [mapBtn setTitleColor:[UIColor c3Color] forState:UIControlStateNormal];
                [mapBtn setTitle:intromodle.label forState:UIControlStateNormal];
                [mapBtn sizeToFit];
                [cell addSubview:mapBtn];
                //设置button的frame
                  mapBtn.frame = CGRectMake(w, 0,mapBtn.width , 25);
                   w = mapBtn.width + mapBtn.frame.origin.x+12;
                //按钮点击方法
                [mapBtn addTarget:self action:@selector(mapBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            }
            
            return cell;
        }
        if (indexPath.row==3) {
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.backgroundColor=[UIColor clearColor];
            UITextView *content=[[UITextView alloc]init];
            content.showsVerticalScrollIndicator=NO;
            content.editable=NO;
            content.backgroundColor=[UIColor clearColor];
            content.frame=CGRectMake(41, 0, KSCREENWIDTH-82, 233);
            content.textColor=[UIColor c3Color];
            content.font=[UIFont font14];
            content.text=self.introModel.content;
            [cell addSubview:content];
            return cell;
        }
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        if (indexPath.row==0) {
             return 189;
        }else if (indexPath.row==1){
            return 47;
        }else if (indexPath.row==2){
            return 50;
        }else if (indexPath.row==3){
            return 233;
        }
    }
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==0) {
        UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 20, KSCREENWIDTH, 66)];
        headerView.backgroundColor=[UIColor clearColor];
        //返回按钮
        UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        backBtn.frame=CGRectMake(10, 13, 35, 35);
        [backBtn setImage:[UIImage imageNamed:@"wallet-back"] forState:UIControlStateNormal];
        [backBtn setBackgroundColor:[UIColor clearColor]];
        [backBtn sizeToFit];
        [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
        //标题
        UILabel *headerLable=[[UILabel alloc]initWithFrame:CGRectZero];
        headerLable.text=@"画单";
        headerLable.font=[UIFont font18];
        headerLable.textColor=[UIColor c3Color];
        [headerLable sizeToFit];
        headerLable.centerX=KSCREENWIDTH/2;
        headerLable.top=backBtn.top;
        [headerView addSubview:headerLable];
        //编辑按钮
        UIButton *editorBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [editorBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [editorBtn setTitleColor:[UIColor c3Color] forState:UIControlStateNormal];
        [editorBtn sizeToFit];
        editorBtn.top=headerLable.top;
        editorBtn.right=headerView.right-15;
        editorBtn.titleEdgeInsets=UIEdgeInsetsMake(-10, 0, 0, 0);
        [editorBtn addTarget:self action:@selector(editorBtnClick) forControlEvents:UIControlEventTouchUpInside];
        if ([self.introModel.ifEdit integerValue]==0) {
            editorBtn.hidden=YES;
        }else{
            editorBtn.hidden=NO;
        }
        [headerView addSubview:editorBtn];
        
        [headerView addSubview:backBtn];
        
        return headerView;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 66;
    }
    return 0.01f;
}
-(void)mapBtnClick:(UIButton*)btn{
    HomeHDTypeVC *vc=[[HomeHDTypeVC alloc]init];
    vc.type=lstid[btn.tag-1000];
    [UIViewController ig_pushViewController:vc animated:YES];
}
-(void)backBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)editorBtnClick{
    
}
-(void)dealloc{
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
